/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import clases.Comprobante;
import clases.Pago;
import clases.TipoPago;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author root
 */
public class MdlPago implements Modelo<Pago>{

    /**
     * 
     * @param pago
     * @param cnx
     * @throws SQLException 
     */
    @Override
    public void insert(Pago pago, Conexion cnx) throws SQLException {
        String sql = "INSERT INTO pago (tipoPago, monto, comprobante, estado) "
                + "VALUES(" + pago.getTipoPago().getIdTipoPago() + ", "
                + pago.getMonto() + ", "
                + pago.getComprobante().getIdComprobante() + ", "
                + "'" + pago.getEstado() + "');";
        cnx.ejecutar(sql);
    }

    /**
     * 
     * @param cnx
     * @return
     * @throws SQLException 
     */
    @Override
    public int getLastId(Conexion cnx) throws SQLException {
        String sql = "SELECT MAX(idPago) FROM pago;";
        ResultSet rs = cnx.consultar(sql);
        if (rs.next()) {
            return rs.getInt(1);
        }
        return -1;
    }

    /**
     * 
     * @param pago
     * @param cnx
     * @throws SQLException 
     */
    @Override
    public void update(Pago pago, Conexion cnx) throws SQLException {
        String sql = "UPDATE pago SET "
                + "tipoPago = " + pago.getTipoPago().getIdTipoPago() + ", "
                + "monto = " + pago.getMonto() + ", "
                + "comprobante = " + pago.getComprobante().getIdComprobante() + ", "
                + "estado = '" + pago.getEstado() + "' "
                + "WHERE idPago = " + pago.getIdPago();
        cnx.ejecutar(sql);
    }

    /**
     * 
     * @param query
     * @param cnx
     * @return
     * @throws SQLException 
     */
    @Override
    public ArrayList<Pago> get(String query, Conexion cnx) throws SQLException {
        ResultSet rs = cnx.consultar(query);
        ArrayList<Pago> pagos = new ArrayList<>();
        while(rs.next()){
            pagos.add(new Pago(
                    rs.getInt(1),
                    new TipoPago(){
                        {
                            this.setIdTipoPago(rs.getInt(2));
                        }
                    },
                    rs.getFloat(3),
                    new Comprobante(){
                        {
                            this.setIdComprobante(rs.getInt(4));
                        }
                    },
                    rs.getString(5).charAt(0)
            ));
        }
        return pagos;
    }

   /**
    * 
    * @param id
    * @param cnx
    * @return
    * @throws SQLException 
    */ 
    @Override
    public Pago get(int id, Conexion cnx) throws SQLException {
        String sql = "SELECT * FROM pago WHERE idPago LIKE " + id;
        ResultSet rs = cnx.consultar(sql);
        Pago pago = null;
        if (rs.next()) {
            pago = new Pago(
                    rs.getInt(1),
                    new TipoPago(){
                        {
                            this.setIdTipoPago(rs.getInt(2));
                        }
                    },
                    rs.getFloat(3),
                    new Comprobante(){
                        {
                            this.setIdComprobante(rs.getInt(4));
                        }
                    },
                    rs.getString(5).charAt(0)
            );
        }
        return pago;
    }
    
}
